<?php

Loader::using("models::entities::CursoPk");
Loader::using("models::entities::Cursos");

/**
 * Description of ListaEstudiante
 *
 * @author MFerreira
 * @export Modules
 * @export-metadata RequiredRoles:ROLE_PROFESSOR
 */
class EvaluarCurso extends Module {

    public function index() {

        $NIT = $this->currentUser->getName();

        $Cursos = $this->model->courses->GetByDocente($NIT);

        $this->assign("Cursos", $Cursos);
        $this->assign("action", $this->route->action("loadStudents"));

        $this->display("index");
    }

    public function loadStudents() {
        Loader::using("models::entities::CursoPk");
        Loader::using("models::entities::PeriodoAcademico");

        $periodo = new PeriodoAcademico();
        $periodo->setAno($this->request->get("Ano"));
        $periodo->setPeriodo($this->request->get("Periodo"));

        $CursoPk = new CursoPk();

        $CursoPk->setCodigoMateria($this->request->get("CodigoMateria"));
        $CursoPk->setCodigoPlan($this->request->get("CodigoPlan"));
        $CursoPk->setGrupo($this->request->get("Grupo"));
        $CursoPk->setSemestre($this->request->get("Semestre"));
        $CursoPk->setPerdiodoAcademico($periodo);

        $Curso = $this->model->courses->GetById($CursoPk);
        $Estudiantes = $this->model->students->GetListaEstudianteByCurso($CursoPk);

        $this->assign("Estudiantes", $Estudiantes);
        $this->assign("Curso", $Curso);
        $this->assign("action", $this->route->action("doEvaluarCurso"));
        $this->assign("cancel", $this->route->action("index"));

        $this->display();
    }

    public function doEvaluarCurso() {
        if ($this->request->submitted()) {
            Loader::using("models::entities::CursosMatriculadosPk");

            $CursoPk = new CursoPk();

            $PeriodoAcademico = new PeriodoAcademico();

            $PeriodoAcademico->setAno($this->request->post("Ano"));
            $PeriodoAcademico->setPeriodo($this->request->post("Periodo"));

            $CursoPk->setPerdiodoAcademico($PeriodoAcademico);
            $CursoPk->setCodigoMateria($this->request->post("CodigoMateria"));
            $CursoPk->setCodigoPlan($this->request->post("CodigoPlan"));
            $CursoPk->setGrupo($this->request->post("Grupo"));
            $CursoPk->setSemestre($this->request->post("Semestre"));

            $Corte = $this->request->post("Corte");

            $Notas = $this->request->post("Notas");

            foreach ($Notas as $codigo => $Nota) {

                $CursoMatriculadoPk = new CursosMatriculadosPk();

                $CursoMatriculadoPk->setCursoPk($CursoPk);
                $CursoMatriculadoPk->setCodigoEstudiante($codigo);

                $this->model->calification->EvaluarCurso($CursoMatriculadoPk, $Nota, $Corte);
            }

            $this->addMessage(new Message("Los alumnos fueron calificados correctamente.", "", Message::SUCCESS));
        }

        $this->index();
    }

}

?>
